var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    district1:[],
    arr_district1:[],
    index1:0,

    district2:[],
    arr_district2:[],
    index2:0,

    district3:[],
    arr_district3:[],
    index3:0,

    address: {
      Id:'',
      ProvinceID:0,
      CityID:0,
      PartID:0,
      
      UserName:'',
      Mobile:'',
      Address:'',
      CreateID: '',
      GPS_Y:0,
      GPS_X:0,
      IsDefault: 0
    },
    addressId:'',
    
    userInfo: null,
    loc:''
  },
  bindPickerChange1:function(e)
  {
    var that=this;
    var index1=e.detail.value;
    that.setData({index1:e.detail.value});
    var district1=that.data.district1;
    var address=that.data.address;
    address.ProvinceID=district1[index1].ID;
    that.setData({address:address});
    that.get_jd(address.ProvinceID);
  },
  bindPickerChange2:function(e)
  {
    var that=this;
    var index2=e.detail.value;
    that.setData({index2:e.detail.value});
    var district2=that.data.district2;
    var address=that.data.address;
    address.CityID=district2[index2].ID;
    that.setData({address:address});
    that.get_sq(address.CityID);
  },
  bindPickerChange3:function(e)
  {
    var that=this;
    var index3=e.detail.value;
    that.setData({index3:e.detail.value});

    var district3=that.data.district3;
    var address=that.data.address;
    address.PartID=district3[index3].ID;
    that.setData({address:address});
     
  },
  bindinputMobile(event) {
    let address = this.data.address;
    address.Mobile = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputName(event) {
    let address = this.data.address;
    address.UserName = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputAddress (event){
    let address = this.data.address;
    address.Address = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindIsDefault(){
    let address = this.data.address;
    address.IsDefault = !address.IsDefault;
    this.setData({
      address: address
    });
  },
  getAddressDetail() {
    let that = this;
    if (that.data.addressId!='')
    {
      util.request(api.AddressDetail, { id: that.data.addressId },'GET').then(function (res) {
        if (res.Status === 100) 
        {
          if (res.Data) 
          {
            var address=res.Data;
            that.setData({ address: address });
            util.request(api.GetDistrictList,{Pid:1},'GET').then(function (res) {
              if (res.Status== 100) 
              {
                that.setData({district1:res.Data});
                var arr=new Array();
                res.Data.map(function(item,i)
                {
                  if(address.ProvinceID==item.ID)
                  {
                    that.setData({index1:i});
                  }
                  arr.push(item.Name);
                });
                that.setData({arr_district1:arr});
                
                 
                util.request(api.GetDistrictList,{Pid:address.ProvinceID},'GET').then(function (res1) {
                  if (res1.Status== 100) 
                  {
                    that.setData({district2:res1.Data});
                    var arr=new Array();
                    res1.Data.map(function(item,i){
                      if(address.CityID==item.ID)
                      {
                        that.setData({index2:i});
                      }
                      arr.push(item.Name);
                    });
                    that.setData({arr_district2:arr});
                    
                     
                    util.request(api.GetDistrictList,{Pid:address.CityID},'GET').then(function (res3) {
                      if (res3.Status== 100) 
                      {
                        that.setData({district3:res3.Data});
                        var arr=new Array();
                        res3.Data.map(function(item,i){
                          if(address.PartID==item.ID)
                          {
                            that.setData({index3:i});
                          }
                          arr.push(item.Name);
                        });
                        that.setData({arr_district3:arr});
                      }
                    });
                  }
                });
              }
            });

          }
        }
      });
    }
    
  },
   
   
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.id) 
    {
      this.setData({addressId: options.id});
      this.getAddressDetail();
    }
    else
    {
      this.get_district();
    }
    var userInfo=wx.getStorageSync('userInfo');
    if(userInfo!="")
    {
      this.setData({ userInfo: userInfo});
    }
    
     
    
  },
  onReady: function () {

  },
  get_jd:function(pid)
  {
    var that=this;
    var address = this.data.address;
    util.request(api.GetDistrictList,{Pid:pid},'GET').then(function (res) {
      if (res.Status== 100) 
      {
        that.setData({district2:res.Data});
        var arr=new Array();
        res.Data.map(function(item){
          arr.push(item.Name);
        });
        that.setData({arr_district2:arr});
        that.setData({index2:0});
        address.CityID = res.Data[0].ID;
        that.setData({address:address});
      }
    });
  },
  get_sq:function(pid)
  {
    var that=this;
    var address = this.data.address;
    util.request(api.GetDistrictList,{Pid:pid},'GET').then(function (res) {
      if (res.Status== 100) 
      {
        that.setData({district3:res.Data});
        var arr=new Array();
        res.Data.map(function(item){
          arr.push(item.Name);
        });
        that.setData({arr_district3:arr});
        that.setData({index3:0});
        address.PartID = res.Data[0].ID;
        that.setData({address:address});
      }
    });
  },
  get_district:function()
  {
    var that=this;
    var info = this.data.address;
    
    util.request(api.GetDistrictList,{Pid:1},'GET').then(function (res) {
      if (res.Status== 100) 
      {
        that.setData({district1:res.Data});
        var arr=new Array();
        res.Data.map(function(item){
          arr.push(item.Name);
        });
        that.setData({arr_district1:arr});
        info.ProvinceID = res.Data[0].ID;
        
        var pid=res.Data[0].ID;
        util.request(api.GetDistrictList,{Pid:pid},'GET').then(function (res1) {
          if (res1.Status== 100) 
          {
            that.setData({district2:res1.Data});
            var arr=new Array();
            res1.Data.map(function(item){
              arr.push(item.Name);
            });
            that.setData({arr_district2:arr});
            info.CityID = res1.Data[0].ID;
            
            var cid=res1.Data[0].ID;
            util.request(api.GetDistrictList,{Pid:cid},'GET').then(function (res3) {
              if (res3.Status== 100) 
              {
                that.setData({district3:res3.Data});
                var arr=new Array();
                res3.Data.map(function(item){
                  arr.push(item.Name);
                });
                that.setData({arr_district3:arr});
                info.PartID = res3.Data[0].ID;
                that.setData({ info: info });
              }
            });
          }
        });
      }
    });

    
  },
  
  cancelAddress(){
    wx.navigateBack({
      url: '/pages/ucenter/address/address',
    })
  },
  saveAddress(){
    let address = this.data.address;
    let that = this;
    if (address.UserName == '') {
      util.showErrorToast('请输入姓名');
      return false;
    }

    if (address.Mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }

    if (address.Address == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }
    var userid='';
    if(that.data.userInfo!='')
    {
      userid = that.data.userInfo.Id;
    }
    var gps_x=0;
    var gps_y=0;
    if(that.data.loc!='')
    {
      gps_x=loc.longitude;
      gps_y=loc.latitude;
    } 

    var url = api.AdressAdd;
    if (address.Id!='')
    {
      url = api.AdressUpdate;
    }
    
    util.request(url, { 
      Id: address.Id,
      UserName: address.UserName,
      Mobile: address.Mobile,
      CreateID: userid,
      Address: address.Address,
      IsDefault: address.IsDefault?1:0,
      GPS_X:gps_x,
      GPS_Y: gps_y,
      ProvinceID:address.ProvinceID,
      CityID:address.CityID,
      PartID:address.PartID
    }, 'POST').then(function (res) {
      if (res.Status === 100) 
      {
        wx.navigateBack({delta: 1});
      }
    });

  },
  onShow: function () {
    // 页面显示
    

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})